package uk.co.threekelvin.perihelion.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Map;

public class Keyboard implements KeyListener {
	public final String[] inputs = { "left", "right", "up", "down" };
	public Map<String, Integer> keyMap = new HashMap<String, Integer>();

	private boolean[] keys = new boolean[222];
	private Map<String, Integer> defKeyMap = new HashMap<String, Integer>();

	private void setDefaultKeys() {
		defKeyMap.put("left", KeyEvent.VK_A);
		defKeyMap.put("right", KeyEvent.VK_D);
		defKeyMap.put("up", KeyEvent.VK_W);
		defKeyMap.put("down", KeyEvent.VK_S);
	}

	public void keyPressed(KeyEvent e) {
		keys[e.getKeyCode()] = true;
	}

	public void keyReleased(KeyEvent e) {
		keys[e.getKeyCode()] = false;
	}

	public void keyTyped(KeyEvent e) {

	}

	public void mapKey(int key, String input) {
		for (String v : inputs) {
			if (input == v) {
				keyMap.put(input, key);
				break;
			}
		}
	}

	public boolean isKeyPressed(int key) {
		if (key < 0 || key > (keys.length - 1)) return false;
		return keys[key];
	}

	public boolean isInputPressed(String input) {
		int key = keyMap.get(input);

		return isKeyPressed(key);
	}

	public Keyboard() {
		setDefaultKeys();
		for (String v : inputs) {
			keyMap.put(v, defKeyMap.get(v));
		}
	}
}
